/**
 * @author jouyang
 */
QS_SecurityChartComponent = function(mainticker,reg,language){
	this.chartComponent = null;
	this.securityType = 0; //stock:0,fund:1.
	this.securityValidation = null;
	this.reg = reg;
	this.language = language;
	this.path;
	this.outId;
	this.uiConfig;
	this.mainticker = mainticker;
	this.securityValidation;
};
QS_SecurityChartComponent.prototype = {
	initOption:function(outId,uiConfig,cfg,id){  // remove the path parameter.
		this.id = id;
		this.outId = outId;
		this.uiConfig = uiConfig;
		this.profile = cfg;
		this.securityValidation = new tickerValidation(100);
		if(typeof(this.mainticker)=="undefined"||this.mainticker==""||this.mainticker==null){ // stock chart;
			if(this.profile.securityType=="FO"){ // init fund chart
			    this.securityType = 1;
				this.chartComponent = new QS_FundChartComponent("",this.reg,this.language);
			}else if(this.profile.securityType == 20){ // forex chart
                this.chartComponent = new QS_ForexChartComponent("",this.reg,this.language);
                this.securityType = 20;
            }else{                               // init stock chart.
				this.chartComponent = new RTChartComponent("",this.reg,this.language);
				this.securityType = 0;
			}
			this.chartComponent.id = this.id;
			this.chartComponent.setTitle = this.setTitle;
			this.chartComponent.__winObj = this.__winObj;
			this.chartComponent.contentMask = this.contentMask;
			this.chartComponent.showLoading = this.showLoading;
			this.chartComponent.hideLoading = this.hideLoading;
			this.chartComponent.tValidation = new tickerValidation(1);
			this.chartComponent.initUIConfig(this.uiConfig);
			this.chartComponent.Init(this.outId,"",this.profile);
		}else{
			var t = this.mainticker;
			this.mainticker="";
			this.acceptTicker(t);
			
		}
	},
	initUIConfig:function(uiConfig){
		if(this.chartComponent!=null){
			this.chartComponent(uiConfig);
		}
	},
	Init:function(outID,config){ 
		if(this.chartComponent!=null){
			this.chartComponent.Init(outID,"",config);
		}
	},
	acceptTicker:function(symbol){       // only switch main ticker
		var self = this;
		if(symbol==this.mainticker)return false;
		this.securityValidation.checkTickers(symbol,function(o){
			if(o!=null&&o.length){
				if(gData[o[0]]){
					var ty = gData[o[0]]["mType"];
                    if(gData[o[0]]["type"]&&gData[o[0]]["type"]==20)ty = "FX";
					var ticker = ty=="FO"?gData[o[0]]["secId"]:gData[o[0]]["performanceId"]
					if(self.mainticker==ticker){
						return false
					}else {
						self.mainticker=ticker;
					}
					var oldType = self.securityType;
					if(ty=="FO")self.securityType = 1; // fund ticker.
					else if(ty=="FX")self.securityType = 20; // forex chart.
					else self.securityType = 0; // stock,etf or index ticker.
					if(oldType!=self.securityType||self.chartComponent == null){	//switch chart type.
						if(oldType!=self.securityType&&self.chartComponent != null){
							self.profile = {width:self.chartComponent.chartWidth,height:self.chartComponent.outHeight+28,
								simpleMenu:self.chartComponent.simpleMenu};
							self.profile=$.extend(true,self.profile,self.chartComponent._getCommonConfig());
						}
						if(self.securityType == 0){
							self.chartComponent = new RTChartComponent(self.mainticker,self.reg,self.language);
						}else if(self.securityType == 1){
							self.chartComponent = new QS_FundChartComponent(self.mainticker,self.reg,self.language);
						}else if(self.securityType == 20){ // forex chart
                            self.chartComponent = new QS_ForexChartComponent(self.mainticker,self.reg,self.language);
                        }
						self.chartComponent.id = self.id;
						self.chartComponent.setTitle = self.setTitle;
						self.chartComponent.__winObj = self.__winObj;
						self.chartComponent.contentMask = self.contentMask;
						self.chartComponent.showLoading = self.showLoading;
						self.chartComponent.hideLoading = self.hideLoading;
						self.chartComponent.tValidation = new tickerValidation(1);
						self.chartComponent.initUIConfig(self.uiConfig);
						self.chartComponent.Init(self.outId, "", self.profile)	
					}else{
						self.chartComponent.acceptTicker(self.mainticker);	
					}
				}
			}else{ // the ticker is invald.
                if(self.chartComponent&&self.chartComponent.__winObj&&self.chartComponent.__winObj.searchEl){
                   if(self.chartComponent.mainTicker&&self.chartComponent.tickerInfos[self.chartComponent.mainTicker]){
                      self.chartComponent.__winObj.searchEl.find("#"+self.chartComponent.__winObj._autoBoxID).val(self.chartComponent.tickerInfos[self.chartComponent.mainTicker]["ticker"]);
                   }
                }
            }
		});
	},
	initUIConfig:function(config){
		if(this.chartComponent!=null){
			this.chartComponent.initUIConfig(config);
		}
	},
	saveConfig:function(config){
		if(this.chartComponent!=null){
			return this.chartComponent.saveConfig(config);
		}else{
			return{};		
		}
	},
	changeOutSize:function(w,h){
		if(this.chartComponent!=null){
			this.chartComponent.changeOutSize(w,h);
		}
	},
	autoCPCallback:function(){
		
	},
	refreshLastTradeDate:function(sdate,ldate){
		if (this.chartComponent != null&&typeof(this.chartComponent.refreshLastTradeDate)=="function") {
			this.chartComponent.refreshLastTradeDate(sdate,ldate);
		}
	},
	refreshIntradayChart:function(type){
		if (this.chartComponent != null&&typeof(this.chartComponent.refreshIntradayChart)== "function") {
			this.chartComponent.refreshIntradayChart(type);
		}
	},
	fileMenuCallback:function(val){ 
	    if (this.chartComponent != null) {
			this.chartComponent.fileMenuCallback(val);
		}
	},
	clear:function(){
		if(this.chartComponent!=null){
			this.chartComponent.clear();
		}
	}
}
